#include"commn.h"

int main()
{
  //创建命名管道
  umask(0);
  if(mkfifo(MY_FIFO,0666) < 0)
  {
    perror("mkfifo fail\n");
    return 1;
  }
  //进行文件的相关操作
  int fd = open(MY_FIFO,O_RDONLY);
  if(fd<0)
  {
    perror("open fail\n");
    return 2;
  }
  //进行读写
  while(1)
  {
    char buffer[64]={0};

    ssize_t sz = read(fd,buffer,sizeof(buffer)-1);
    if(sz>0)
    {
      buffer[sz]=0;
      if(strcmp(buffer,"show")==0)
      {
        if(fork()==0)
        {
          execl("/usr/bin/ls","ls","-l",NULL);
          exit(-1);
        }

        waitpid(-1,NULL,0);
      }
      else if(strcmp(buffer,"run")==0)
      {
        if(fork()==0)
        {
          execl("/usr/bin/sl","sl",NULL);
          exit(-1);
        }

        waitpid(-1,NULL,0);
      }
      else
      {
        printf("client# %s\n",buffer);
      }
    }
    else if(sz==0)
    {
      printf("client quit\n");
      break;
    }
    else
    {
      perror("read fail\n");
      break;
    }
  }

  close(fd);
  return 0;
}
